//
//  DR_AdConfig.swift
//  DocReaderRP
//
//  Created by  mac on 2025/11/19.
//

import Foundation

//远端广告配置Key
enum DR_AdConfigValue: String {
    case remoteConfigKey = "DR_adconfig"
    case fbConfigKey = "DR_fbConfig"
}

// MARK: - 广告配置相关模型
struct DR_AdTypeConfig: Codable {
    let type: String
    let cached: Int
    let ids: [DR_AdID]
}

struct DR_AdID: Codable {
    let id: String
    let priority: Int
}

struct DR_AdSceneConfig: Codable {
    let scene: String
    let sceneModel: Int
}

// MARK: - 完整广告配置模型
struct DR_AdConfigModel: Codable {
    let usermodel: String
    let valueCoefficient: Double
    let launchAdType: String
    let isCountdown: String
    let AdCountdown: Int
    let adTypeConfig: [DR_AdTypeConfig]
    let adSceneConfig: [DR_AdSceneConfig]
    
    // 获取用户模型整数
    var userModelInt: Int {
        return Int(usermodel) ?? 2
    }
    
    // 自定义解码，支持 AdCountdown 为字符串或整数
    enum CodingKeys: String, CodingKey {
        case usermodel
        case valueCoefficient
        case launchAdType
        case isCountdown
        case AdCountdown
        case adTypeConfig
        case adSceneConfig
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        
        usermodel = try container.decode(String.self, forKey: .usermodel)
        valueCoefficient = try container.decode(Double.self, forKey: .valueCoefficient)
        launchAdType = try container.decode(String.self, forKey: .launchAdType)
        isCountdown = try container.decode(String.self, forKey: .isCountdown)
        adTypeConfig = try container.decode([DR_AdTypeConfig].self, forKey: .adTypeConfig)
        adSceneConfig = try container.decode([DR_AdSceneConfig].self, forKey: .adSceneConfig)
        
        // AdCountdown 可能是字符串或整数，需要特殊处理
        if let intValue = try? container.decode(Int.self, forKey: .AdCountdown) {
            AdCountdown = intValue
        } else if let stringValue = try? container.decode(String.self, forKey: .AdCountdown),
                  let intValue = Int(stringValue) {
            AdCountdown = intValue
        } else {
            // 如果都解析失败，使用默认值 5
            AdCountdown = 5
        }
    }
}

//sceneModel 1.买量用户和普通用户都展示 0.买量用户展示、普通用户不展示   -1.买量用户和普通用户都不展示
//usermodel 1.买量用户 2.普通用户
//valueCoefficient 上报价值系数
//launchAdType 启动页广告类型 1.开屏 2.插屏 3.全屏原生
//isCountdown 全屏原生广告配置支持配置是否倒计时（默认是） 0.否 1.是
//AdCountdown 倒计时时长（默认 5s）
let adConfig_dic =
"""
{
    "usermodel": "0",
    "valueCoefficient": 0.5,
    "launchAdType": “1”,
    "isCountdown": “1”,
    "AdCountdown": 5,
    "adTypeConfig": [
        {
            "type": "open",
            "cached": 1,
            "ids": [
                {
                    "id": "ca-app-pub-3940256099942544/5575463023",
                    "priority": 99
                }
            ]
        },
        {
            "type": "interstitial",
            "cached": 2,
            "ids": [
                {
                    "id": "ca-app-pub-3940256099942544/4411468910",
                    "priority": 99
                },
                {
                    "id": "ca-app-pub-3940256099942544/1033173712",
                    "priority": 60
                },
                {
                    "id": "ca-app-pub-1508498335109884/5760407804",
                    "priority": 30
                }
            ]
        },
        {
            "type": "native",
            "cached": 2,
            "ids": [
                {
                    "id": "ca-app-pub-3940256099942544/3986624511",
                    "priority": 99
                },
                {
                    "id": "ca-app-pub-3940256099942544/2247696110",
                    "priority": 88
                }
            ]
        }
    ],
    "adSceneConfig": [
        {
            "scene": "launch",
            "sceneModel": 1
        },
        {
            "scene": "language_inter",
            "sceneModel": 1
        },
        {
            "scene": "language_native",
            "sceneModel": 1
        },
        {
            "scene": "home_inter",
            "sceneModel": 1
        },
        {
            "scene": "homeFunction_inter",
            "sceneModel": 1
        },
        {
            "scene": "play_inter",
            "sceneModel": 1
        },
        {
            "scene": "add_inter",
            "sceneModel": 1
        },
        {
            "scene": "import_native",
            "sceneModel": 1
        },
        {
            "scene": "import_inter",
            "sceneModel": 1
        },
        {
            "scene": "artists_native",
            "sceneModel": 1
        },
        {
            "scene": "list_native",
            "sceneModel": 1
        },
        {
            "scene": "album_native",
            "sceneModel": 1
        },
        {
            "scene": "setting_native",
            "sceneModel": 1
        }
    ]
}
"""
